/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever;

import General.Globals;
import General.RequestMaker;
import SVNSupport.SVNManager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.tmatesoft.svn.core.SVNLogEntry;

/**
 *
 * @author Manuel
 */
public class SourceForgeRetriever extends Retriever{
    private SVNManager svnManager;

    public SourceForgeRetriever(DataManager mgr) {
        super(mgr);
        this.forgeName = "http://sourceforge.net";
        svnManager = new SVNManager();
    }
    
    public void retrieveContextData() {
        currentProject.setUrl(this.currentLink);
        currentProject.setForge("sourceforge");
        currentProject.setName(this.parseCharacterData("projects/","/",this.currentLink,false,true));
        currentProject.setDescription(this.parseCharacterData("<article id=\"project-description\">","</article>",this.currentPage,true,true));
        this.retrieveLabels();
        this.retrieveCollaborators();
    }


    public void retrieveLabels(){
        String blockString = this.parseCharacterData("<aside id=\"project-secondary\">", "</aside>", this.currentPage, false,true);
        String tagStart = "<section class=\"project-info\">";
        String tagEnd = "</section>";
        String infoTag = "<section class=\"content\">";
        String infoTagEnd = "</a>";
        int startIndex = blockString.indexOf(tagStart);
        while(startIndex != -1){
            String infoBlock = this.parseCharacterData(tagStart, tagEnd, blockString, false,true);
            if(infoBlock.toLowerCase().contains("topic")){
                currentProject.addLabel(this.parseCharacterData(infoTag, infoTagEnd, infoBlock, true,true));
            }

            else if(infoBlock.toLowerCase().contains("language")){
                currentProject.addLanguage(this.parseCharacterData(infoTag, infoTagEnd, infoBlock, true,true));
            }

            blockString = blockString.substring(blockString.indexOf(tagStart)+10);
            startIndex = blockString.indexOf(tagStart);
        }

    }



    private void retrieveCollaborators() {

        //find the user page
        String groupID = parseCharacterData("group_id=","\"",this.currentPage,false,true);
        //getting the site which lists the users
        String userPage = RequestMaker.getPage("http://sourceforge.net/project/memberlist.php?group_id="+groupID);

        //check through all links and retrieve user id
        userPage = this.parseCharacterData("</thead>", "</table>", userPage, false,true).toLowerCase();
        int nextLink = userPage.indexOf("<td align=\"middle\">");
        String currentCollaborator;
        while(nextLink != -1){
            userPage = userPage.substring(nextLink);
            currentCollaborator = this.parseCharacterData("<td align=\"middle\">", "</td>",userPage, false,true);
            if(currentCollaborator.contains("/users/")){
                currentCollaborator = this.filterHtml(currentCollaborator);
            currentProject.addCollaboratorIfNew(currentCollaborator);
            }
            nextLink = userPage.indexOf("<td align=\"middle\">",userPage.indexOf("<td align=\"middle\">")+10);
        }
    }

    
    public ProjectPackage selectArchieveLink() {
        //TODO implement
        ProjectPackage pkg = new ProjectPackage();

        //retrieve the package name
        String downloadsPage = RequestMaker.getPage(this.currentProject.getUrl()+"files");
        String area = this.parseCharacterData("Looking for the latest version?", "class=", downloadsPage, false, true);
        String link = this.parseCharacterData("href=\"", "\"", area, false, true);

        if(link.endsWith("/download"))
            link = link.substring(0,link.lastIndexOf("/"));

        boolean isInterestingLink =  link.endsWith(".tar.bzip2") || link.endsWith(".tbz2") || link.endsWith(".tar.bz2") || link.endsWith(".tar.gzip") || link.endsWith(".tgz") || link.endsWith(".tar.gz") || link.endsWith("zip") || link.endsWith(".tar") || link.endsWith(".jar") || link.endsWith(".class");
        if(link == null || link.isEmpty() || !isInterestingLink)
            return null;
        pkg.setName(link);
        
        //retrieve the date of upload
        String date = parseCharacterData("released on ", " ", area, false, true);
        DateFormat df = DateFormat.getDateInstance();
        Date myDate = new Date();
        if(!date.contains("-"))
            System.out.println("PROBLEM DATE NOT GIVEN: "+ currentProject.getUrl());
        myDate.setYear(Integer.parseInt(date.substring(0,date.indexOf("-")))-1900);
        myDate.setMonth(Integer.parseInt(date.substring(date.indexOf("-")+1,date.lastIndexOf("-"))));
        myDate.setDate(Integer.parseInt(date.substring(date.lastIndexOf("-")+1)));
        pkg.setDate(myDate);
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        if(link == null || link.isEmpty())
            return null;
        else
            return pkg;

    }

    public String selectRepositoryLink() {
        String url = "";
        String repoPage = RequestMaker.getPage(currentProject.getUrl()+"develop");

        // if it is SVN
        boolean checkout = repoPage.contains("svn checkout");
        boolean co = repoPage.contains("svn co");
        if(checkout || co){
            if(checkout)
                url = "svn"+this.parseCharacterData("svn checkout ", " ", repoPage, true, true);
            else if(co)
                url = "svn"+this.parseCharacterData("svn co ", " ", repoPage, true, true);
        }
        return url;
    
    }


}
